using System;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using Microsoft.SPOT;

namespace Melody_Glider.GUIContent
{
    internal class GUI
    {
        //can these be private?
        internal readonly MainMenuWindow MainMenuWindow;
        internal readonly EditorWindow EditorWindow;
        internal readonly LoadMenuWindow LoadMenuWindow;
        internal readonly NumericalInputWindow NumericalInputWindow;
        internal readonly NoteInputWindow NoteInputWindow;
        internal readonly DeleteQuestionBox DeleteQuestionBox;
        internal readonly CancelQuestionBox CancelQuestionBox;
        internal readonly Cursor Cursor;

        public GUI(SystemState state)
        {
            MainMenuWindow = new MainMenuWindow(this, state);
            EditorWindow = new EditorWindow(this, state);
            LoadMenuWindow = new LoadMenuWindow(this, state);
            NumericalInputWindow = new NumericalInputWindow(this, state);
            NoteInputWindow = new NoteInputWindow(this, state);
            DeleteQuestionBox = new DeleteQuestionBox(this, state);
            CancelQuestionBox = new CancelQuestionBox(this, state);
            Cursor = new Cursor(-1);
        }

        public void Initialize() //how to shorthen this? another hashtable??? I'm not familiar with them and mama said "don't trust strangers!"
        {
            long totalTicks;
            long ticks;
            
            ticks = DateTime.Now.Ticks;
            MainMenuWindow.Initialize(Resources.GetString(Resources.StringResources.MainMenu));
            ticks = DateTime.Now.Ticks - ticks;
            totalTicks = ticks;
            Debug.Print("MainMenuWindow loaded in " + ticks / TimeSpan.TicksPerMillisecond + "ms");

            ticks = DateTime.Now.Ticks;
            EditorWindow.Initialize(Resources.GetString(Resources.StringResources.Editor));
            ticks = DateTime.Now.Ticks - ticks;
            totalTicks += ticks;
            Debug.Print("EditorWindow loaded in " + ticks / TimeSpan.TicksPerMillisecond + "ms");

            ticks = DateTime.Now.Ticks;
            LoadMenuWindow.Initialize(Resources.GetString(Resources.StringResources.LoadMenu));
            ticks = DateTime.Now.Ticks - ticks;
            totalTicks += ticks;
            Debug.Print("LoadMenuWindow loaded in " + ticks / TimeSpan.TicksPerMillisecond + "ms");

            ticks = DateTime.Now.Ticks;
            NumericalInputWindow.Initialize(Resources.GetString(Resources.StringResources.NumericalInput));
            ticks = DateTime.Now.Ticks - ticks;
            totalTicks += ticks;
            Debug.Print("NumericalInputWindow loaded in " + ticks / TimeSpan.TicksPerMillisecond + "ms");

            ticks = DateTime.Now.Ticks;
            NoteInputWindow.Initialize(Resources.GetString(Resources.StringResources.NoteInput));
            ticks = DateTime.Now.Ticks - ticks;
            totalTicks += ticks;
            Debug.Print("NoteInputWindow loaded in " + ticks / TimeSpan.TicksPerMillisecond + "ms");

            ticks = DateTime.Now.Ticks;
            DeleteQuestionBox.Initialize(Resources.GetString(Resources.StringResources.DeleteQuestionBox));
            ticks = DateTime.Now.Ticks - ticks;
            totalTicks += ticks;
            Debug.Print("DeleteQuestionBox loaded in " + ticks / TimeSpan.TicksPerMillisecond + "ms");

            ticks = DateTime.Now.Ticks;
            CancelQuestionBox.Initialize(Resources.GetString(Resources.StringResources.CancelQuestionBox));
            ticks = DateTime.Now.Ticks - ticks;
            totalTicks += ticks;
            Debug.Print("CancelQuestionBox loaded in " + ticks / TimeSpan.TicksPerMillisecond + "ms");

            Debug.Print("GUI loaded in " + totalTicks / TimeSpan.TicksPerMillisecond + "ms");

            GlideTouch.Initialize();

            MainMenuWindow.SetToMain();
        }

        public void DispMsg(string entry)
        {
            MainMenuWindow.DispMsg(entry);
        }

        public void ToggleElement(string elementName, Window window, bool enabled) //window needed because sometimes it is used on window before setting it to Glide.MainWindow - otherwise null reference exception
        {
            var element = window.GetChildByName(elementName);
            element.Enabled = enabled;
            //btn.DisabledFontColor = ColorUtility.ColorFromRGB(0x00, 0x00, 0x00); //todo: to xml - waiting for Glide release which will solve this issue
                                                                                   //todo: or do something that also note buttons will have the same color
            window.FillRect(element.Rect);
            element.Invalidate();
        }
    }
}
